package br.com.transitojp.bean;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

import br.com.transitojp.controller.UserControl;
import br.com.transitojp.model.User;

@ManagedBean
@RequestScoped
public class UserBean {

	private User user;
	private String login;
	private String password;
	private UserControl userControl = new UserControl();
	
	public String logar(){
		
		User userLogged = userControl.validateLogin(login, password);
		
		HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
	        
	    if(userLogged!= null){           
	    	session.setAttribute("user", userLogged); 
	    	return "loginSucess"; 
	    } 
	    else{
	     	return "loginFail";  
	    }  
		
	}
	
	public String logout(){
		 HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false); 
         session.removeAttribute("user"); 
         return "loginFail"; 
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public String getLogin() {
		return login;
	}

	public void setLogin(String login) {
		this.login = login;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public UserControl getUserControl() {
		return userControl;
	}

	public void setUserControl(UserControl userControl) {
		this.userControl = userControl;
	}
	
}
